/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import lombok.Data;
import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.modules.system.domain.SysDispatchUnit;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.SysDispatchUnitService;
import me.zhengjie.modules.system.domain.vo.SysDispatchUnitQueryCriteria;
import me.zhengjie.modules.system.mapper.SysDispatchUnitMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author qiang
* @date 2023-09-13
**/
@Service
@RequiredArgsConstructor
public class SysDispatchUnitServiceImpl extends ServiceImpl<SysDispatchUnitMapper, SysDispatchUnit> implements SysDispatchUnitService {

    private final SysDispatchUnitMapper sysDispatchUnitMapper;

    @Override
    public PageResult<SysDispatchUnit> queryAll(String name , String type, Page<Object> page){
        List<SysDispatchUnit> units = sysDispatchUnitMapper.findAllUnit(name , type, page);
        return PageUtil.toPage(units);
    }



    @Override
    public List<SysDispatchUnit> queryAll(SysDispatchUnitQueryCriteria criteria){
        return sysDispatchUnitMapper.findAll(criteria);
    }
 /**@Description: 查询树结构
  * @return
  * @version v1.0
  * @date 2023/10/5 0:34
  */
    @Override
    public List<SysDispatchUnit> queryTree(SysDispatchUnitQueryCriteria criteria) {
        List<SysDispatchUnit> sysDispatchUnitList = sysDispatchUnitMapper.selectList(null);

        /****
         * 1.先过滤根节点0
         * 2. 然后根据根节点找下级节点
         * 3递归调用 将子节点放到对应的子代
         */
        List<SysDispatchUnit> collect = sysDispatchUnitList.stream().filter(f -> f.getPid().equals("0")).map(item -> {
            item.setChildern(findChildern(item, sysDispatchUnitList));
            return item;


        }).collect(Collectors.toList());
        return collect;
    }
/**@Description: 递归寻找子代
 * @param
 * @return
 * @version v1.0
 * @date 2023/10/5 0:34
 */
    private List<SysDispatchUnit> findChildern(SysDispatchUnit item, List<SysDispatchUnit> sysDispatchUnitList) {
        List<SysDispatchUnit> collect = sysDispatchUnitList.stream().filter(
                f -> {
                    return f.getPid().equals(item.getOid());
                }
        ).map(m -> {
            m.setChildern(findChildern(m, sysDispatchUnitList));
            return m;
        }).collect(Collectors.toList());
        return  collect;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SysDispatchUnit resources) {
        resources.setCreateTime(new Timestamp(new Date().getTime()));
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDispatchUnit resources) {
        SysDispatchUnit sysDispatchUnit = getById(resources.getId());
        sysDispatchUnit.copy(resources);
        saveOrUpdate(sysDispatchUnit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
       removeBatchByIds(ids);
    }

    @Override
    public ArrayList<DictDetail> getShortName() {
        return sysDispatchUnitMapper.getShortName();
    }

    @Override
    public ArrayList<String> getTypeNames() {
        return sysDispatchUnitMapper.getTypeNames();
    }

    @Override
    public void download(List<SysDispatchUnit> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysDispatchUnit sysDispatchUnit : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", sysDispatchUnit.getName());
            map.put("关联组织机构", sysDispatchUnit.getOid());
            map.put("上级调度单位", sysDispatchUnit.getPid());
            map.put("调度性质", sysDispatchUnit.getType());
            map.put("简称", sysDispatchUnit.getShortName());
            map.put("创建者", sysDispatchUnit.getCreateBy());
            map.put("更新者", sysDispatchUnit.getUpdateBy());
            map.put("创建时间", sysDispatchUnit.getCreateTime());
            map.put("更新时间", sysDispatchUnit.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}